/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.modelo.Autor;

/**
 *
 * @author Juan Carlos
 */
@ManagedBean
@SessionScoped
public class AuthorBean {

    //-------------------------EJB's--------------------------------------------
    @EJB
    private AutorBlLocal autorBl;
    //--------------------------------------------------------------------------

    //----------------------------VARIABLES-------------------------------------
    private List<Autor> lista;
    private Autor author;
    private String accion="";

    //--------------------------------------------------------------------------
    
    //---------------------Constructores----------------------------------------
    public AuthorBean() {
        author = new Autor();
    }
    //--------------------------------------------------------------------------

    //----------------------GETTER and SETTER-----------------------------------
    public List<Autor> getLista() {
        lista=autorBl.listAll();
        return lista;
    }

    public void setLista(List<Autor> lista) {
        this.lista = lista;
    }

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
    //--------------------------------------------------------------------------

    //----------------Acciones--------------------------------------------------
    public String procesarAccion() {
        System.out.println("---------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>><"+accion);
        if (accion.equalsIgnoreCase("Nuevo")) {
            autorBl.register(author);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                autorBl.modify(author);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    autorBl.delete(author);
                }
            }
        }
        accion = "";
        return "AuthorList";
    }

    public void prepararNuevo(ActionEvent evt) {
        System.out.println("--------------------------------------------"+accion);
        accion = "Nuevo";
        author = new Autor();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author = lista.get(index);
    }

    public String llamadaPaginas() {
        String link = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            link = "AuthorCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                link = "AuthorList";
            }
        }
        return link;
    }
    //--------------------------------------------------------------------------
}
